package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.mapper.StrategyCatalogMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService{
    @Autowired
    private StrategyCatalogMapper strategyCatalogMapper;

    @Override
    public void saveOrUpdate(StrategyCatalog entity) {
        //如果没有序号,子默认为数据库中最大的序列号
        if (entity.getSequence() == null) {
          int maxSequence = strategyCatalogMapper.selectMaxSequence(entity.getId());
          entity.setSequence(maxSequence+1);
        }

        if (entity.getId() == null) {
            strategyCatalogMapper.insert(entity);

        }else{
            strategyCatalogMapper.updateByPrimaryKey(entity);

        }
    }

    @Override
    public PageInfo list(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<StrategyCatalog> list = strategyCatalogMapper.selectForList(qo);
        return new PageInfo<>(list);
        
    }

    @Override
    public StrategyCatalog selectById(Long id) {
        return strategyCatalogMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<StrategyCatalog> listByStrategyId(Long strategyId) {
        return strategyCatalogMapper.selectByStrategyId(strategyId);
    }


}
